package com.yunbiao.ybboardapi.utils.timeset;

import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.yunbiao.ybboardapi.DeviceType;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SystemTimeContral {
    private static final String TAG = "SystemTimeContral";
    private static SystemTimeContral timeContral;

    private SystemTimeContral() {
    }

    public static SystemTimeContral getInstance() {
        if (timeContral == null)
            timeContral = new SystemTimeContral();
        return timeContral;
    }

    public boolean setTime(Context context,long systemTime, DeviceType deviceType) {
        if (context==null) return false;
        boolean isOk=false;
        switch (deviceType) {
            case YISHENG:
                isOk=yishene(context,systemTime);
                break;
            case MSTAR:
                isOk=qingji(context,systemTime);
                break;
            default:
                isOk=shimeitai(systemTime);
                break;
        }
        return isOk;
    }

    private boolean qingji(Context context,long time){
        if (context==null) return false;
        Intent intent = new Intent("android.intent.action.qt.modifytime");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
//        int[] timeArray = {2019,9,25,13,14,15};
        int[] timeArray = new int[6];
        timeArray[0]=calendar.get(Calendar.YEAR);
        timeArray[1]=calendar.get(Calendar.MONTH)+1;
        timeArray[2]=calendar.get(Calendar.DAY_OF_MONTH);
        timeArray[3]=calendar.get(Calendar.HOUR_OF_DAY);
        timeArray[4]=calendar.get(Calendar.MINUTE);
        timeArray[5]=calendar.get(Calendar.SECOND);

        intent.putExtra("timeParameters",timeArray);
        context.sendBroadcast(intent);

        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return isToday(time);
    }

    private boolean yishene(Context context,long time){
        if (context==null) return false;
        Intent intent = new Intent();
        intent.setAction("com.ys.update_time");
        intent.putExtra("current_time", time);
        if (!Build.MODEL.contains("rk")) {
            intent.setPackage("com.ys.ys_receiver");
        }
        context.sendBroadcast(intent);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return isToday(time);
    }



    private boolean shimeitai(long time){
        try {
            TimeSetUtil.setDateTime(time);
            return isToday(time);
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return false;
    }

    private boolean isToday(long systemTime){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm");

        String deviceDateStr = format.format(Calendar.getInstance().getTime());
        String serviceDateStr = format.format(new Date(systemTime));

        if (deviceDateStr.equals(serviceDateStr)){
            return true;
        }

        return false;
    }

}
